<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>点名系统</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: Arial, sans-serif;
        }
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            background-color: #f0f2f5;
        }
        .container {
            width: 100%;
            max-width: 400px;
            background: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            text-align: center;
            transition: transform 0.3s ease;
        }
        .container:hover {
            transform: scale(1.02);
        }
        h1 {
            font-size: 1.5em;
            color: #333;
            margin-bottom: 15px;
        }
        label {
            font-size: 1em;
            color: #555;
        }
        input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-top: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 1em;
            transition: border-color 0.3s ease;
        }
        input[type="text"]:focus {
            border-color: #007bff;
        }
        button {
            margin-top: 15px;
            padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1em;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: #0056b3;
        }
        .message {
            margin-top: 20px;
            font-size: 1em;
            color: #333;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>点名系统</h1>
    <form id="attendanceForm">
        <label for="name">请输入姓名：</label>
        <input type="text" id="name" name="name" required>
        <button type="submit">提交</button>
    </form>
    <div class="message" id="message"></div>
</div>

<script>
    document.getElementById("attendanceForm").addEventListener("submit", function(event) {
        event.preventDefault();  // 阻止默认表单提交行为
        const name = document.getElementById("name").value;
        const messageDiv = document.getElementById("message");

        if (name.trim() === "") {
            messageDiv.textContent = "姓名不能为空！";
            messageDiv.style.color = "red";
            return;
        }

        // 使用AJAX发送请求
        fetch("/submit", {
            method: "POST",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            body: `name=${encodeURIComponent(name)}`
        })
        .then(response => response.ok ? "提交成功！" : "提交失败，请重试！")
        .then(message => {
            messageDiv.textContent = message;
            messageDiv.style.color = message === "提交成功！" ? "green" : "red";
            document.getElementById("name").value = "";  // 清空输入框
        })
        .catch(error => {
            messageDiv.textContent = "提交失败，请重试！";
            messageDiv.style.color = "red";
        });
    });
</script>
</body>
</html>
